'''
Created on Jun 12, 2012

@author: thomas_dao
'''

from google.appengine.ext import ndb
from entity.entity import Poll, Option

@ndb.tasklet
def get_by_event(event_key, **q_options):
    qry = Poll.query(Poll.event == event_key).order(-Poll.created_time)
    polls = yield qry.fetch_async(20, **q_options)
    
    options = []
    users = []
    for poll in polls:
        qry = Option.query(Option.poll == poll.key).order(-Option.created_time)
        poll_options = yield qry.fetch_async()
        
        for option in poll_options:
            option_users = yield ndb.get_multi_async(option.users)
            users.append(option_users)
            
        options.append(poll_options)
        
    raise ndb.Return(polls, options, users)